<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>throw</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C/C++ Keywords</a> &gt; <a href="throw.html">throw</a>
  </div>

  <div class="name-format">
    throw
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  try {
  statement list;
  }
  catch( typeA arg ) {
  statement list;
  }
  catch( typeB arg ) {
  statement list;
  }
  ...
  catch( typeN arg ) {
  statement list;
  }
</pre>

  <p>The throw statement is part of the C++ mechanism for exception
  handling. This statement, together with the <a href=
  "try.html">try</a> and <a href="catch.html">catch</a> statements, the
  C++ exception handling system gives programmers an elegant mechanism
  for error recovery.</p>

  <p>You will generally use a <a href="try.html">try</a> block to
  execute potentially error-prone code. Somewhere in this code, a throw
  statement can be executed, which will cause execution to jump out of
  the <a href="try.html">try</a> block and into one of the <a href=
  "catch.html">catch</a> blocks. For example:</p>
  <pre class="example-code">
   try {
     cout &lt;&lt; &quot;Before throwing exception&quot; &lt;&lt; endl;
     throw 42;
     cout &lt;&lt; &quot;Shouldn&#39;t ever see this&quot; &lt;&lt; endl;
   }
   catch( int error ) {
     cout &lt;&lt; &quot;Error: caught exception &quot; &lt;&lt; error &lt;&lt; endl;
   }            
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="catch.html">catch</a><br>
    <a href="try.html">try</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
